package com.qdevelop.model.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class ArrayUtils {
	
	private static ArrayUtils au = new ArrayUtils();
	public static ArrayUtils getInstance(){
		return au;
	}

	/**
	 * Conver List<Integer> to int[].
	 * @param ints
	 * @return
	 */
	public  int[] convert(Collection<Integer> ints) {
		int[] result = new int[ints.size()];
		int i = 0;
		Iterator<Integer> iter = ints.iterator();
		while (iter.hasNext()) {
			result[i] = iter.next();
		}
		return result;
	}

	/**
	 * merge inputed two int[] arrays.
	 * 
	 * @param array1
	 * @param array2
	 * @return
	 * @throws NullPointerException
	 *             if <code>array1</code> or <code>array2</code> is null.
	 */
	public  int[] merge(int[] array1, int[] array2) {
		if (array1 == null || array2 == null) {
			throw new NullPointerException();
		}

		// check arguments
		if (array1.length == 0) {
			return array2;
		}
		if (array2.length == 0) {
			return array1;
		}

		// initialize arguments
		int[] array = new int[array1.length + array2.length];
		int pos1 = 0;
		int pos2 = 0;
		int pos = 0;

		// loop and merge
		while (pos1 < array1.length && pos2 < array2.length) {
			if (array1[pos1] < array2[pos2]) {
				array[pos++] = array1[pos1++];
				if (pos1 == array1.length) {
					System.arraycopy(array2, pos2, array, pos, array2.length
							- pos2);
					pos = pos + array2.length - pos2;
				}
			} else if (array1[pos1] == array2[pos2]) {
				array[pos++] = array1[pos1];
				pos1++;
				pos2++;
				if ((pos1 == array1.length) && (pos2 == array2.length)) {
					break;
				}
				if (pos1 == array1.length) {
					System.arraycopy(array2, pos2, array, pos, array2.length
							- pos2);
					pos = pos + array2.length - pos2;
				}
				if (pos2 == array2.length) {
					System.arraycopy(array1, pos1, array, pos, array1.length
							- pos1);
					pos = pos + array1.length - pos1;
				}
			} else {
				array[pos++] = array2[pos2++];
				if (pos2 == array2.length) {
					System.arraycopy(array1, pos1, array, pos, array1.length
							- pos1);
					pos = pos + array1.length - pos1;
				}
			}
		}

		// prepare return variable
		int[] result = new int[pos];
		System.arraycopy(array, 0, result, 0, pos);
		return result;
	}

	
     public ArrayList<Integer> intersectArrayWithSeq(List<Integer> l1,List<Integer> l2){
    	HashSet<Integer> hs = new HashSet<Integer>(l1);
    	HashSet<Integer> hs1 = new HashSet<Integer>(l2);
    	ArrayList<Integer> al = new ArrayList<Integer>();
    	HashSet<Integer> hs2 = new HashSet<Integer>();
    	for(int i = 0; i < l1.size(); i++){
    		Integer o = l1.get(i);
    		if(!hs2.contains(o)&& (hs.contains(o) && hs1.contains(o))){
    			al.add(o);
    		}
    		hs2.add(o);
    	}
    	return al;
    }
    
	/**
	 * intersect inputed two int[] arrays.
	 * 
	 * @param array1
	 * @param array2
	 * @return
	 * @throws NullPointerException
	 *             if <code>array1</code> or <code>array2</code> is null.
	 */
	public  List<Integer> intersect(Integer[] array1, Integer[] array2) {
		if (array1 == null || array2 == null) {
			throw new NullPointerException();
		}

		// check arguments
		if (array1.length == 0 || array2.length == 0) {
			return new ArrayList<Integer>();
		}

		// initialize arguments
		List<Integer> array = new ArrayList<Integer>(Math.min(array1.length, array2.length));
		int pos1 = 0;
		int pos2 = 0;

		while (pos1 < array1.length && pos2 < array2.length) {
			if (array1[pos1].compareTo(array2[pos2]) < 0) {
				pos1++;
				if (pos1 == array1.length) {
					break;
				}
			} else if (array1[pos1].compareTo(array2[pos2]) == 0) {
				array.add(array1[pos1]);
				pos1++;
				pos2++;
				if ((pos1 == array1.length) || (pos2 == array2.length)) {
					break;
				}
			} else {
				pos2++;
				if (pos2 == array2.length) {
					break;
				}
			}
		}
		return array;
	}
	
	
	
	public  boolean equals(byte[] b1, byte[] b2) {
		if (b1 == b2) {
			return true;
		}
		if (b1 == null || b2 == null) {
			return false;
		}
		if (b1.length != b2.length) {
			return false;
		}
		for (int i = 0; i < b1.length; i++) {
			if (b1[i] != b2[i]) {
				return false;
			}
		}
		return true;
	}
	
	public void toPrint(int[] result){
		for(int i=0;i<result.length;i++){
			System.out.println(i+":   "+result[i]);
		}
	}
	
	public static void main(String[] args){
		int[] a = new int[]{13,2,23,4,24,16};
		int[] b = new int[]{21,12,1,43,13};
		int [] tmp = ArrayUtils.getInstance().merge(a, b);
		ArrayUtils.getInstance().toPrint(tmp);
	}

}
